﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using InformationSystem.Framework.Dao;
using InformationSystem.BLL.Core.Model;
using Spring.Data.Common;
using System.Data;

namespace InformationSystem.BLL.Core.Dao.Impl
{
    public class SystemDaoImpl : InfoSysAdoDaoSupport, ISystemDao
    {
        public void CreateTraffic(Traffic traffic)
        {
            string insertSql = @"if not exists(
	                        select 1 from TrafficLog 
	                        where vtimeyear=@vtimeyear and vtimemonth=@vtimemonth and vtimeday=@vtimeday)
	                        insert into TrafficLog (vtimeyear,vtimemonth,vtimeday,vcount) 
	                        values (@vtimeyear,@vtimemonth,@vtimeday,@vcount)";
            IDbParametersBuilder builder = CreateDbParametersBuilder();
            builder.Create().Name("vtimeyear").Type(DbType.Int32).Value(traffic.Year);
            builder.Create().Name("vtimemonth").Type(DbType.Int32).Value(traffic.Month);
            builder.Create().Name("vtimeday").Type(DbType.Int32).Value(traffic.Day);
            builder.Create().Name("vcount").Type(DbType.Int32).Value(traffic.Count);
            AdoTemplate.ExecuteNonQuery(CommandType.Text, insertSql, builder.GetParameters());

        }

        public IList<Traffic> GetTrafficByMonth(int year, int month)
        {
            throw new NotImplementedException();
        }

        public IList<Traffic> GetTrafficByYear(int year)
        {
            throw new NotImplementedException();
        }


        public void RegisterSystem(SysRegEnvironment sys)
        {
            string deletesql = "delete from SystemData";
            string insertSql = "insert into SystemData (sysid,regKey,regdata,signkey,regtime) values (@sysid,@regKey,@regdata,@signkey,@regtime)";
            AdoTemplate.ExecuteNonQuery(CommandType.Text, deletesql);
            IDbParametersBuilder builder = CreateDbParametersBuilder();
            builder.Create().Name("sysid").Type(DbType.Int32).Value(1);
            builder.Create().Name("regKey").Type(DbType.String).Value(sys.RegID);
            builder.Create().Name("regdata").Type(DbType.Binary).Value(sys.RegData);
            builder.Create().Name("signkey").Type(DbType.Binary).Value(sys.VerifyData);
            builder.Create().Name("regtime").Type(DbType.DateTime).Value(sys.RegTime);
            AdoTemplate.ExecuteNonQuery(CommandType.Text, insertSql, builder.GetParameters());
        }

        public SysRegEnvironment ReadSystemInfo()
        {
            string selectSql = "select sysid,regKey,regdata,signkey,regtime  from SystemData where sysid=1";
            DataTable dt = AdoTemplate.ClassicAdoTemplate.DataTableCreate(CommandType.Text, selectSql);
            if (dt.Rows.Count == 0) return null;
            SysRegEnvironment sys = new SysRegEnvironment();
            sys.RegID = dt.Rows[0]["regKey"].ToString();
            sys.RegData = (byte[])dt.Rows[0]["regdata"];
            sys.VerifyData = (byte[])dt.Rows[0]["signkey"];
            sys.RegTime = (DateTime)dt.Rows[0]["regtime"];

            return sys;
        }
    }
}
